package org.hncors.gnssparse.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.hncors.gnssparse.MainActivity;
import org.hncors.gnssparse.R;
import org.hncors.gnssparse.widget.CustomProgressDialog;
import org.hncors.gnssparse.widget.MyToast;
import org.xutils.x;


/**
 * Created by wenshi on 2016/4/26.
 */
public abstract class BaseFragment extends Fragment {
    private LinearLayout base_main;
    private LinearLayout base_top;
    protected View mainview;
    private LayoutInflater inflater;
    private ViewGroup container;

    private CustomProgressDialog m_ProgressDialog;
    protected MyApplication application;
    protected MainActivity mActivity = null;
  //  public TextView topRightText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application = MyApplication.getInstance();
        mActivity = (MainActivity)this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mainview =  inflater.inflate(R.layout.activity_base_frame, null);
        base_main = (LinearLayout)mainview.findViewById(R.id.base_main);
        base_top = (LinearLayout) mainview.findViewById(R.id.base_top);
        this.inflater = inflater;
        this.container = container;
        return mainview;
    }

    protected void appendMainBody(Object object, int pResID) {
        View view = inflater.inflate(pResID, null);
        x.view().inject(object,view);
        RelativeLayout.LayoutParams _LayoutParams = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        base_main.addView(view,_LayoutParams);

    }

    /**
     * 包含返回和设置操作顶部栏
     */
    protected void appendTopBody(int pResID) {
        base_top.setVisibility(View.VISIBLE);
        View view = inflater.inflate(pResID,null);
        base_top.addView(view);
       // topRightText = (TextView) view.findViewById(R.id.top_right_text);
    }

    protected void setTopBarTitle(String pText) {
        TextView tvTitle = (TextView)mainview.findViewById(R.id.top_center_text);
        if (tvTitle!=null) {
            tvTitle.setText(pText);
        }
    }

    protected void setTopLeftGone() {
        ImageButton tvTitle = (ImageButton)mainview.findViewById(R.id.top_left);
        if (tvTitle!=null) {
            tvTitle.setVisibility(View.GONE);
        }
    }



    /**
     * 弹出提示信息
     */
    public void showMsg(String pMsg) {
        MyToast m = MyToast.makeText(this.getActivity(), pMsg, 1000);
        m.setGravity(Gravity.BOTTOM, 0, 200);
        m.show();
    }

    /**
     * 弹出加载框 不带文字
     */
    public CustomProgressDialog showProgressDialog() {
        if(m_ProgressDialog==null ){
            m_ProgressDialog = CustomProgressDialog.createDialog(this.getContext());
            m_ProgressDialog.setMessage("请稍等");
            m_ProgressDialog.setCanceledOnTouchOutside(false);
            m_ProgressDialog.show();
        }
        if(m_ProgressDialog!=null && !m_ProgressDialog.isShowing()){
            m_ProgressDialog.setMessage("请稍等");
            m_ProgressDialog.setCanceledOnTouchOutside(false);
            m_ProgressDialog.show();
        }
        return m_ProgressDialog ;
    }

    /**
     * 隐藏加载框
     */
    public void dismissProgressDialog() {
        if (m_ProgressDialog != null) {
            m_ProgressDialog.dismiss();
        }
    }
}
